package com.example.homecunter.video;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.utils.BaseEntity;
import com.example.homecunter.R;
import com.example.homecunter.news.NewsSunFragment;
import com.example.homecunter.news.adapter.NewsAdapter;
import com.example.homecunter.news.newsentity.NewsEntity;
import com.example.homecunter.video.adapter.VideoAdapter;
import com.example.homecunter.video.entity.VideoEntity;
import com.example.homecunter.video.viewmodel.ChannelViewModel;
import com.example.mvvmcore.fragment.BaseMVVMFragment;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

public class GsyvSunFragment extends BaseMVVMFragment<ChannelViewModel> {

    View view;
    private SmartRefreshLayout gsyvSmart;
    private RecyclerView gsyvSunRv;
    int page = 1;
    VideoAdapter videoAdapter;
    List<VideoEntity> videoData;


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        view = super.onCreateView(inflater, container, savedInstanceState);
        initView();
        return view;
    }

    @Override
    protected void initData() {

        gsyvSmart = (SmartRefreshLayout) view.findViewById(R.id.gsyv_smart);
        gsyvSunRv = (RecyclerView) view.findViewById(R.id.gsyv_sun_rv);



    }

    @Override
    protected Class getViewModelClazz() {
        return ChannelViewModel.class;
    }

    @Override
    protected void initEnv() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gsyv_sun;
    }

    @Override
    protected void preparVarMap(Map<Integer, Object> map) {

    }

    @Override
    protected void stopLoad() {

    }

    @Override
    protected void lazyLoad() {

        int types = this.getArguments().getInt("types");
        Toast.makeText(getContext(), "+"+types, Toast.LENGTH_SHORT).show();
        mViewModel.video(types, page).observe(this, new Observer<BaseEntity<List<VideoEntity>>>() {
            @Override
            public void onChanged(BaseEntity<List<VideoEntity>> listBaseEntity) {
                videoData = listBaseEntity.getData();

                videoAdapter = new VideoAdapter(getContext(), videoData);
                gsyvSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                gsyvSunRv.setAdapter(videoAdapter);
            }
        });


        /**
         * 上拉加载  下拉刷新
         */
        gsyvSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mViewModel.video(types,page).observe(GsyvSunFragment.this, new Observer<BaseEntity<List<VideoEntity>>>() {
                    @Override
                    public void onChanged(BaseEntity<List<VideoEntity>> listBaseEntity) {

                        List<VideoEntity> data = listBaseEntity.getData();
                        VideoAdapter videoAdapter = new VideoAdapter(getContext(), data);
                        gsyvSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        gsyvSunRv.setAdapter(videoAdapter);

                    }
                });
                gsyvSmart.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                videoData.clear();
                page = 1;
                mViewModel.video(types,page).observe(GsyvSunFragment.this, new Observer<BaseEntity<List<VideoEntity>>>() {
                    @Override
                    public void onChanged(BaseEntity<List<VideoEntity>> listBaseEntity) {

                        List<VideoEntity> data = listBaseEntity.getData();
                        VideoAdapter videoAdapter = new VideoAdapter(getContext(), data);
                        gsyvSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        gsyvSunRv.setAdapter(videoAdapter);

                    }
                });
                gsyvSmart.finishRefresh();

            }
        });
    }

    private void initView() {

    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_gsyv_sun, container, false);
//    }
}